package comviva_2014.SmartMapper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

public class Main {
	static final IntMap txnDataMap = new IntMap(25000000);
	static final IntMap refDataMap = new IntMap(25000000);

	static byte b = 0;

	public static void main(String[] args) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				System.in));

		String[] line = reader.readLine().split(" ");
		String fileName = line[0];
		File f = new File(fileName);

		if (f.exists() == false) {
			System.out.println("FAILURE");
			return;
		}

		loadDataMap(fileName);
		if (line.length == 1) {
			System.out.println("READY");
			return;
		}

		String[] txn_ids_csv = line[1].split("=")[1].split(",");

		List<String> resList = new ArrayList<String>();

		int hash = -1;
		byte[] token = null;
		;

		for (String txn_id : txn_ids_csv) {
			hash = genHashCode(txn_id.substring(3).getBytes());
			;
			// System.out.println("t hash: " + txn_hash);
			token = txnDataMap.get(hash);
			if (token != null)
				resList.add("REF" + new String(unPack(token)));
			else
				resList.add("FAILURE");
		}

		String[] ref_ids_csv = line[2].split("=")[1].split(",");
		token = null;
		hash = -1;
		for (String ref_id : ref_ids_csv) {
			hash = genHashCode(ref_id.substring(3).getBytes());
			token = refDataMap.get(hash);
			if (token != null)
				resList.add("TXN" + new String(unPack(token)));
			else
				resList.add("FAILURE");
		}

		StringBuilder res = new StringBuilder();
		for (int i = 0; i < resList.size(); i++) {
			if (resList.get(i) == null) {
				res.append("FAILURE");
			} else {
				res.append(resList.get(i));
			}
			if (i < resList.size() - 1)
				res.append(",");

		}
		System.out.print(res);

	}

	static int genHashCode(byte[] b_arr) {
		int result = 1;
		for (byte b : b_arr) {
			result = 31 * result + (b - 47);
		}
		return result;

	}

	private static void loadDataMap(String fileName) throws IOException {

		FileChannel inChannel = new FileInputStream(fileName).getChannel();

		byte[] bb = new byte[1024 * 64];

		ByteBuffer buffer = ByteBuffer.wrap(bb);

		int txn_hash = -1;
		byte[] txn_packed = null;
		byte[] token = new byte[15];
		int token_hash = 1;
		int token_p = 0;
		byte b = 0;
		int j = 0;

		int bread = 0;

		while ((bread = inChannel.read(buffer)) > 0) {
			for (int ib = 0; ib < bread; ib++) {
				b = bb[ib];
				if (b == ',') {
					txn_hash = token_hash;
					txn_packed = token;
					token_p = 0;
					token = new byte[15];

					token_hash = 1;
					j = 0;
				} else if (b == '\n') {
					if (token_p == 0)
						continue;
					// System.out.println("Before put");
					txnDataMap.put(txn_hash, token);
					refDataMap.put(token_hash, txn_packed);

					// ref_bb = Arrays.copyOf(token, 29);
					token_p = 0;
					token = new byte[15];
					token_hash = 1;
					j = 0;

				} else {
					if (b < '0' || b > '9')
						continue;
					// token[token_p++] = b;

					if ((token_p & 1) == 1) { // odd
						token[j] = (byte) (token[j] | (byte) (0x0F & (b - '0')));
						j++;
					} else {
						token[j] = (byte) ((b - '0') << 4);
					}
					token_p++;

					token_hash = (31 * token_hash) + (b - 47);

				}
			}
			buffer.clear();
		}
		inChannel.close();

	}

	static long djb_hash(byte[] buff, int start, int end) {
		long hash = 5381;

		for (int i = start; i <= end; i++) {
			hash = ((hash << 5) + hash) + buff[i];
		}

		return hash;
	}

	/*
	 * private static byte[] pack(byte[] in) { byte[] pack = new byte[15];
	 * 
	 * int j = 0; int i = 0; for (; i < in.length - 1; i += 2) { pack[j++] =
	 * (byte) ((in[i] - '0') << 4 | (0x0F & (in[i + 1] - '0'))); } pack[j] =
	 * (byte) ((in[i] - '0') << 4); return pack; }
	 */
	private static byte[] unPack(byte[] in) {
		byte[] unpack = new byte[29];

		int p = 0;
		int i = 0;
		for (; i < in.length - 1; i++) {
			unpack[p] = (byte) ((in[i] & 0xF0) >> 4);
			unpack[p + 1] = (byte) (in[i] & 0x0F);

			unpack[p] += '0';
			unpack[p + 1] += '0';
			p += 2;
		}
		unpack[p] = (byte) (((in[i] & 0xF0) >> 4) + '0');

		return unpack;
	}

	static class IntMap {
		static final int DEFAULT_INITIAL_CAPACITY = 16;
		static final int MAXIMUM_CAPACITY = 1 << 30;
		static final float DEFAULT_LOAD_FACTOR = 0.99f;
		Entry[] table;
		int size;
		static int threshold;

		int indexHash;
		int indexFor;
		Entry e;
		int tempHash;

		public IntMap(int initialCapacity) {

			// Find a power of 2 >= initialCapacity
			/*
			 * int capacity = 1; while (capacity < initialCapacity) capacity <<=
			 * 1;
			 */
			// System.out.println("Capacity: " + capacity);

			threshold = (int) (initialCapacity * DEFAULT_LOAD_FACTOR);
			table = new Entry[initialCapacity];
		}

		public void put(int key, byte[] value) {
			// indexHash = hash(key);
			tempHash = key;
			// This function ensures that hashCodes that differ only by
			// constant multiples at each bit position have a bounded
			// number of collisions (approximately 8 at default load factor).
			tempHash ^= (tempHash >>> 20) ^ (tempHash >>> 12);
			indexHash = tempHash ^ (tempHash >>> 7) ^ (tempHash >>> 4);

			indexFor = indexHash & (table.length - 1);
			for (e = table[indexFor]; e != null; e = e.next) {

				if (e.hash == indexHash && e.key == key) {
					e.value = value;
					return;
				}
			}

			e = table[indexFor];
			table[indexFor] = new Entry(indexHash, key, value, e);
			if (size++ >= threshold) {
			/*	System.out.printf(
						"Size: %d, Threshold: %d, Table Length: %d\n", size,
						threshold, table.length); */
				resize(2 * table.length);
			}
			return;
		}

		static int hash(int h) {
			// This function ensures that hashCodes that differ only by
			// constant multiples at each bit position have a bounded
			// number of collisions (approximately 8 at default load factor).
			h ^= (h >>> 20) ^ (h >>> 12);
			return h ^ (h >>> 7) ^ (h >>> 4);
		}

		void addEntry(int hash, int key, byte[] value, int bucketIndex) {
			Entry e = table[bucketIndex];
			table[bucketIndex] = new Entry(hash, key, value, e);
			if (size++ >= threshold)
				resize(2 * table.length);
		}

		void resize(int newCapacity) {
			// Entry[] oldTable = table;
			int oldCapacity = table.length;
			if (oldCapacity == MAXIMUM_CAPACITY) {
				threshold = Integer.MAX_VALUE;
				return;
			}

			Entry[] newTable = new Entry[newCapacity];
			transfer(newTable);
			table = newTable;
			threshold = (int) (newCapacity * DEFAULT_LOAD_FACTOR);
		}

		void transfer(Entry[] newTable) {
			Entry[] src = table;
			Entry next;
			int i = -1;

			int newCapacity = newTable.length;
			for (int j = 0; j < src.length; j++) {
				e = src[j];
				if (e != null) {
					src[j] = null;
					do {
						next = e.next;
						i = e.hash & (newCapacity - 1);
						e.next = newTable[i];
						newTable[i] = e;
						e = next;
					} while (e != null);
				}
			}
		}

		final byte[] get(int key) {
			int hash = hash(key);
			for (Entry e = table[hash & (table.length - 1)]; e != null; e = e.next) {

				if (e.hash == hash && e.key == key)
					return e.value;
			}
			return null;

		}

		static class Entry {
			final int key;
			byte[] value;
			Entry next;
			final int hash;

			Entry(int h, int k, byte[] v, Entry n) {
				value = v;
				next = n;
				key = k;
				hash = h;
			}
		}
	}
}
